package com.hm.shopping.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.shopping.auto.su.BaseDomain;
import com.hm.shopping.domain.QUserDomain;
import com.hm.shopping.domain.UserDomain;
import com.hm.shopping.repository.UserRepository;
import com.hm.shopping.su.BeanUtil;
import com.hm.shopping.vo.UserVo;
import com.hm.shopping.vo.su.UserAddressVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年1月23日 下午5:45:17
 * @version 1.0
 * @describe
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserRepository userRepository;

	@Override
	public void refreshUser(UserVo userVo) {
		if (CommonUtil.isAnyEmpty(userVo.getSubscriberId())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		if (userRepository.exists(QUserDomain.userDomain.subscriberId.eq(userVo.getSubscriberId()))) {
			return;
		} else {
			UserDomain domain = new UserDomain();
			BeanUtil.copyProperties(userVo, domain);
			BaseDomain.initBaseInfo(domain);

			userRepository.save(domain);
		}
	}

	@Override
	public List<UserAddressVo> getUserAddressList(Long subscriberId) {
		UserDomain domain = userRepository.findOne(QUserDomain.userDomain.subscriberId.eq(subscriberId));
		if (CommonUtil.isEmpty(domain)) {
			return new ArrayList<UserAddressVo>();
		}
		return domain.getUserAddress();
	}

	@Override
	public void updateUserAddress(Long subscriberId, UserAddressVo addressVo) {
		log.info("修改用户{}地址参数为{}", subscriberId, addressVo.toString());
		if (CommonUtil.isAnyEmpty(subscriberId, addressVo.getDetailedAddress(), addressVo.getTakeUserName(), addressVo.getTakeUserPhone())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		UserDomain domain = userRepository.findOne(QUserDomain.userDomain.subscriberId.eq(subscriberId));
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		if (StringUtil.isBlankOrNull(addressVo.getAddressId())) {
			List<UserAddressVo> userAddress = domain.getUserAddress().parallelStream().map(item -> {
				item.setRecentlyUse(false);
				return item;
			}).collect(Collectors.toList());

			{
				addressVo.setAddressId(UUID.randomUUID().toString());
				addressVo.setRecentlyUse(true);
				userAddress.add(addressVo);
				domain.setUserAddress(userAddress);
			}
			userRepository.save(domain);
		} else {
			List<UserAddressVo> address = domain.getUserAddress().parallelStream().filter(item -> !(item.getAddressId().equals(addressVo.getAddressId()))).map(item -> {
				item.setRecentlyUse(false);
				return item;
			}).collect(Collectors.toList());

			List<UserAddressVo> userAddress = domain.getUserAddress();
			{
				addressVo.setAddressId(UUID.randomUUID().toString());
				addressVo.setRecentlyUse(true);
				userAddress.add(addressVo);
				domain.setUserAddress(address);
			}
			userRepository.save(domain);
		}
	}

	@Override
	public void settingUserDefaultAddress(Long subscriberId, String addressId) {
		if (CommonUtil.isAnyEmpty(subscriberId, addressId)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		UserDomain domain = userRepository.findOne(QUserDomain.userDomain.subscriberId.eq(subscriberId));
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		domain.setUserAddress(domain.getUserAddress().parallelStream().map(item -> {
			item.setRecentlyUse(item.getAddressId().equals(addressId));
			return item;
		}).collect(Collectors.toList()));

		userRepository.save(domain);
	}

	@Override
	public void deleteUserAddress(Long subscriberId, String addressId) {
		if (CommonUtil.isAnyEmpty(subscriberId, addressId)) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		UserDomain domain = userRepository.findOne(QUserDomain.userDomain.subscriberId.eq(subscriberId));
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		domain.setUserAddress(domain.getUserAddress().parallelStream().filter(item -> !(item.getAddressId().equals(addressId))).collect(Collectors.toList()));
		userRepository.save(domain);
	}

}
